package com.medical.service.impl;

import com.medical.mapper.RegionUsageMapper;
import com.medical.pojo.RegionUsage;
import com.medical.pojo.RegionUsageVO;
import com.medical.service.RegionUsageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RegionUsageServiceImpl implements RegionUsageService {

    @Autowired
    private RegionUsageMapper regionUsageMapper;

    /**
     * 根据RegionUsageVO查询所有区域使用情况
     *
     * @param regionUsageVO 包含查询条件的RegionUsageVO对象
     * @return 区域耗能情况列表
     */
    @Override
    public List<RegionUsage> findAll(RegionUsageVO regionUsageVO) {
        return regionUsageMapper.findAll(regionUsageVO);
    }

    /**
     * 查询所有区域的使用情况，并以字符串列表的形式返回
     *
     * @param regionUsageVO 包含查询条件的RegionUsageVO对象
     * @return 区域耗能情况的字符串列表
     */
    @Override
    public List<String> findRegionUsage(RegionUsageVO regionUsageVO) {
        List<RegionUsage> list = regionUsageMapper.findAll(regionUsageVO);
        List<String> regionUsageList = list.stream().map(RegionUsage::getRegionUsage).collect(Collectors.toList());
        return regionUsageList;
    }

    /**
     * 查询环形图数据，包括每个区域的使用情况和区域名称
     *
     * @param regionUsageVO 包含查询条件的RegionUsageVO对象
     * @return 包含区域耗能情况和区域名称的映射列表，用于生成环形图
     */
    @Override
    public List<Map<String,String>> findAnnularChart(RegionUsageVO regionUsageVO) {
        ArrayList<Map<String,String>> arrayList = new ArrayList<>();
        List<RegionUsage> list = regionUsageMapper.findAll(regionUsageVO);

        List<String> chartUsage = list.stream().map(RegionUsage::getRegionUsage).collect(Collectors.toList());
        List<String> regionName = list.stream().map(RegionUsage::getRegionName).collect(Collectors.toList());

        for (int i = 0; i < chartUsage.size(); i++) {
            HashMap<String, String> map = new HashMap<>();
            map.put("value", chartUsage.get(i));
            map.put("name", regionName.get(i));
            arrayList.add(map);
        }
        return arrayList;
    }
}
